//
//  CJClassSelectedView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/9.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "CHClassTypeSelectedView.h"

@interface CHClassTypeSelectedView () <UIPickerViewDelegate,UIPickerViewDataSource>{
    //班级模型数组
    NSMutableArray *dataArray;
}
@property (strong, nonatomic) UIPickerView *picker;    //选择器
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *minDateStr;
@property (strong, nonatomic) BRClassResultBlock resultBlock;
@property (nonatomic, strong) TeacherClassListModel *selectValue;   //选择的值
@property (strong, nonatomic) NSMutableArray<NSString *> *data;


@end
@implementation CHClassTypeSelectedView
+ (void)showDatePickerWithTitle:(NSString *)title minDateStr:(NSString *)minDateStr resultBlock:(BRClassResultBlock)resultBlock{
    CHClassTypeSelectedView *datePicker = [[CHClassTypeSelectedView alloc] initWithTitle:title minDateStr:minDateStr resultBlock:resultBlock];
    [datePicker showWithAnimation:YES];

}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (instancetype)initWithTitle:(NSString *)title minDateStr:(NSString *)minDateStr resultBlock:(BRClassResultBlock)resultBlock{
    if (self = [super init]) {
        _title = title;
        _minDateStr = minDateStr;
        _resultBlock = resultBlock;
        [self getClassTypeRequest];
        
    }
    return self;
}
//UI布局，主要就是在弹出视图上添加选择器
- (void)initUI{
    [super initUI];
    self.titleLabel.text = _title;
    
    _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,50, SCREEN_WIDTH, kDatePicHeight)];
    _picker.showsSelectionIndicator = YES;
    //设置代理
    _picker.delegate =self;
    _picker.dataSource =self;
    // 添加时间选择器
    [self.alertView addSubview:self.picker];
}
//选择器的初始化和布局
//- (UIPickerView *)picker{
//    if (!_picker) {
//
//    }
//    return _picker;
//}

-(void)getClassTypeRequest{
    _data = [[NSMutableArray alloc] init];
    dataArray = [NSMutableArray new];

    [SYRequestAPI postDataFromURLScheme:@"common/CommonService.svc/GetTemplateTypeList" parameterDictionary:nil success:^(id responseObject) {
        NSArray *arr = responseObject[@"data"];
        for (NSDictionary *dic in arr) {
            TeacherClassListModel *model = [[TeacherClassListModel alloc]init];
            model.name = dic[@"templateTypeName"];
            model.status = dic[@"templateTypeId"];
            [self->dataArray addObject:model];
            [self->_data addObject:model.name];
        }
        [self initUI];
    } fail:^(NSError *error) {
        
    }];
}
//选择器数据的加载，从设定的最小日期到当前月
- (NSMutableArray<NSString *> *)data{
    if (!_data) {

        self.selectValue = [dataArray firstObject];
    }
    return _data;
}
#pragma mark - UIPickerView的数据和布局，和tableview类似
//返回多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.data.count;
}

//每一行的数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.data[row];
}

//选中时的效果
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.selectValue = dataArray[row];
}

//返回高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 35.0f;
}

//返回宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 300*fw;
}

#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    //    [self dismissWithAnimation:NO];
}

#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation {
    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    CGRect rect = self.alertView.frame;
    rect.origin.y = 64;
    self.alertView.frame = rect;
}

#pragma mark - 关闭视图方法
- (void)dismissWithAnimation:(BOOL)animation {
    [self.leftBtn removeFromSuperview];
    [self.rightBtn removeFromSuperview];
    [self.titleLabel removeFromSuperview];
    [self.lineView removeFromSuperview];
    [self.topView removeFromSuperview];
    [self.picker removeFromSuperview];
    [self.alertView removeFromSuperview];
    [self.backgroundView removeFromSuperview];
    [self removeFromSuperview];
}

#pragma mark - 取消按钮的点击事件
- (void)clickLeftBtn {
    [self dismissWithAnimation:YES];
}

#pragma mark - 确定按钮的点击事件
- (void)clickRightBtn {
    if ([_selectValue isEqual:@"null"]||_selectValue == nil) {
        _selectValue = dataArray[0];
    }
    self.resultBlock(_selectValue);
    [self dismissWithAnimation:YES];
}
@end
